package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketChannel;
import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.AdminOrderService;
import com.cskaoyan.market.service.impl.AdminOrderServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/order/*")
public class AdminOrderController extends HttpServlet {

    AdminOrderService adminOrderService=new AdminOrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/order/", "");
        if("list".equals(op)){
            list(req, resp);
        }else if("detail".equals(op)){
            detail(req,resp);
        }else if("channel".equals(op)){
            channel(req,resp);
        }
    }

    private void channel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarketChannel[] orderList = adminOrderService.channel();
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.ok(orderList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        MarketAdmin admin = (MarketAdmin) session.getAttribute("admin");
        String name=admin.getUsername();
        String avatar=admin.getAvatar();
        String id = req.getParameter("id");
        Map<String,Object> data = adminOrderService.detail(id);
        MarketUser user = new MarketUser();
        user.setNickname(name);
        user.setAvatar(avatar);
        data.put("user",user);
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String[] orderStatusArray = req.getParameterValues("orderStatusArray");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        String orderSn = req.getParameter("orderSn");
        String userId = req.getParameter("userId");
        List<MarketOrder> orderList = adminOrderService.list(page, limit,orderStatusArray, sort, order,start,end,orderSn,userId);
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.okList(orderList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/order/", "");
        if("refund".equals(op)){
            refund(req, resp);
        }else if("ship".equals(op)){
            ship(req,resp);
        }else if("delete".equals(op)){
            delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        Integer orderId= JacksonUtil.parseInteger(requestBody,"orderId");
        int i=adminOrderService.delete(orderId);
        Object o ;
        if(i==623){
            o= ResponseUtil.fail(623,"订单不能删除");
        }else{
            o= ResponseUtil.ok(i);
        }
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void ship(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        // 获取参数并转换
        Integer orderId = JacksonUtil.parseInteger(requestBody, "orderId");
        String shipChannel= JacksonUtil.parseString(requestBody,"shipChannel");
        String shipSn= JacksonUtil.parseString(requestBody,"shipSn");
        int i=adminOrderService.ship(orderId,shipChannel,shipSn);
        Object o = ResponseUtil.ok(i);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void refund(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        // 获取参数并转换
        Integer orderId = Integer.valueOf(JacksonUtil.parseString(requestBody, "orderId"));
        Integer refundMoney = Integer.valueOf(JacksonUtil.parseString(requestBody, "refundMoney"));
        int i=adminOrderService.refund(orderId,refundMoney);
        Object o = ResponseUtil.ok(i);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }
}
